﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit.HTMLEditor.ToolbarButton;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRREGIONManager : BasePage
{
    public static bool Insert;
    BUS_WRREGION busWRREGION = new BUS_WRREGION();
    BUS_WRAREA busWrarea = new BUS_WRAREA();
    BUS_WRUSER busWruser = new BUS_WRUSER();
    string currentpage = "1";
    private string cateid = "WRREGIONManager";
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.MaintainScrollPositionOnPostBack = true;
        if (RouteData.Values["currentpage"] != null)
        {
            currentpage = RouteData.Values["currentpage"].ToString();
        }
        if (!IsPostBack)
        {
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindDrop();
            ShowPaging();
        }
    }
    private void ShowPaging()
    {
        bool flag = false;
        string chuoi = "";
        chuoi += "<ul>";
        if (busWRREGION._Count() > 10)
        {
            if ((busWRREGION._Count()) % 10 == 0)
            {
                if (Int32.Parse(currentpage) == 1)
                {
                    chuoi += "<li><a class='li_first' href='/WRMO/1/" + cateid + ".htm'>" + "Prev" + "</a></li>";
                }
                else
                {
                    chuoi += "<li><a class='li_first' href='/WRMO/" + (Int32.Parse(currentpage) - 1).ToString() + "/" + cateid + ".htm'>" + "Prev" + "</a></li>";
                }
                for (int i = 1; i <= (busWRREGION._Count() / 10); i++)
                {

                    if (i % 11 == 0 && Int32.Parse(currentpage) % 10 != 0)
                    {
                        break;
                    }
                    if (Int32.Parse(currentpage)%10 == 0)
                    {
                        if (flag)
                            goto here;
                        flag = true;
                        i = Int32.Parse(currentpage);
                    }
                here:
                    if (i.ToString().Equals(currentpage))
                    {
                        chuoi += "<li><a class='selected' href='/WRMO/" + i.ToString() + "/" + cateid + ".htm'>" + i + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a  href='/WRMO/" + i.ToString() + "/" + cateid + ".htm'>" + i + "</a></li>";
                    }
                }
                if (Int32.Parse(currentpage) == ((busWRREGION._Count() / 10) + 1))
                {
                    chuoi += "<li><a class='li_last' href='/WRMO/" + currentpage + "/" + cateid + ".htm'>" + "Next" + "</a></li>";
                }
                else 
                {
                    chuoi += "<li><a class='li_last' href='/WRMO/" + (Int32.Parse(currentpage) + 1).ToString() + "/" + cateid + ".htm'>" + "Next" + "</a></li>";
                }
            }
            else
            {
                if (Int32.Parse(currentpage) == 1)
                {
                    chuoi += "<li><a class='li_first' href='/WRMO/1/" + cateid + ".htm'>" + "Prev" + "</a></li>";
                }
                else
                {
                    chuoi += "<li><a class='li_first' href='/WRMO/" + (Int32.Parse(currentpage) - 1).ToString() + "/" + cateid + ".htm'>" + "Prev" + "</a></li>";
                }

                for (int i = 1; i <= (busWRREGION._Count() / 10) + 1; i++)
                {
                    if (i % 11 == 0 && Int32.Parse(currentpage) % 10 != 0)
                    {
                        break;
                    }
                    if (Int32.Parse(currentpage) % 10 == 0)
                    {
                        if (flag)
                            goto here;
                        flag = true;
                        i = Int32.Parse(currentpage);
                    }
                here:
                    if (i.ToString().Equals(currentpage))
                    {
                        chuoi += "<li><a class='selected' href='/WRMO/" + i.ToString() + "/" + cateid + ".htm'>" + i + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a href='/WRMO/" + i.ToString() + "/" + cateid + ".htm'>" + i + "</a></li>";
                    }
                }
                if (Int32.Parse(currentpage) == ((busWRREGION._Count() / 10) + 1))
                {
                    chuoi += "<li><a class='li_last' href='/WRMO/" + currentpage + "/" + cateid + ".htm'>" + "Next" + "</a></li>";
                }
                else
                {
                    chuoi += "<li><a class='li_last' href='/WRMO/" + (Int32.Parse(currentpage) + 1).ToString() + "/" + cateid + ".htm'>" + "Next" + "</a></li>";
                }
            }
        }
        chuoi += "</ul>";
        ltrpaging.Text = chuoi;
        grvWRREGION.DataSource = busWRREGION.Get_Reigon_FromTo(Int32.Parse(currentpage) * 10 - 10 + 1, Int32.Parse(currentpage) * 10);
        grvWRREGION.DataBind();
    }
    private void BindGrid()
    {
        grvWRREGION.DataSource = busWRREGION.Get_Reigon_FromTo(1,20);
        grvWRREGION.DataBind();
    }

    public void BindDrop()
    {
        dropIDArea.DataSource = busWrarea._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.AreaName }).ToList();
        dropIDArea.DataTextField = "AreaName";
        dropIDArea.DataValueField = "Id";
        dropIDArea.DataBind();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtRegionName.Text = "";
        txtRegionCode.Text = "";
        txtDescription.Text = "";
        txtKeywords.Text = "";


    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRREGION.Items.Count > 0)
        {
            for (int i = 0; i < grvWRREGION.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRREGION.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRREGION.Items[i].Cells[1].Text;
                    try
                    {
                        busWRREGION._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtRegionName.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập RegionName!");
            txtRegionName.Focus();
            return;
        }
        if (txtRegionCode.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập RegionCode!");
            txtRegionCode.Focus();
            return;
        }
        if (txtDescription.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Description!");
            txtDescription.Focus();
            return;
        }
        if (txtKeywords.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập Keywords!");
            txtKeywords.Focus();
            return;
        }


        WRREGION obj = new WRREGION();

        obj.RegionName = txtRegionName.Text;
        obj.RegionCode = txtRegionCode.Text;
        obj.IDArea = Convert.ToInt32(dropIDArea.SelectedValue);
        obj.Description = txtDescription.Text;
        obj.Active = dropActive.SelectedValue;
        obj.Keywords = txtKeywords.Text;
        obj.UpdateDate = DateTime.Now;
        obj.IDUpdateBy = Convert.ToInt32(Session["userid"].ToString());
        if (Insert)
        {
            obj.IDCreatBy = Convert.ToInt32(Session["userid"].ToString());
            obj.CreateDate = DateTime.Now;
            busWRREGION._Add(obj);
        }
        else
        {
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRREGION._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRREGION_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRREGION _WRREGION = busWRREGION._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            txtRegionName.Text = _WRREGION.RegionName;
            txtRegionCode.Text = _WRREGION.RegionCode;
            try
            {
                dropIDArea.SelectedValue = _WRREGION.IDArea.ToString();
            }
            catch
            {
                Show("Area is not active!");
            }
            txtDescription.Text = _WRREGION.Description;
            dropActive.SelectedValue = _WRREGION.Active.ToString();
            txtKeywords.Text = _WRREGION.Keywords;

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRREGION._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRREGION_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRREGION.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void Linkbutton_Click(object sender, EventArgs e)
    {
        LinkButton lbt = (LinkButton)sender;
        string s = lbt.Text;
        ShowPaging();
    }
}
